package com.jing.yao.modules.user.controller;

import com.jing.yao.bean.KeyValue;
import com.jing.yao.bean.ResponseBean;
import com.jing.yao.component.controller.YaoBaseController;
import com.jing.yao.component.service.YaoBaseService;
import com.jing.yao.holder.UserContentHolder;
import com.jing.yao.modules.resource.dao.entity.ResourceEntity;
import com.jing.yao.modules.resource.service.ResourceService;
import com.jing.yao.modules.role.service.RoleService;
import com.jing.yao.modules.user.controller.dto.UserDTO;
import com.jing.yao.modules.user.controller.param.UserParam;
import com.jing.yao.modules.user.dao.entity.UserEntity;
import com.jing.yao.modules.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户controller
 *
 * @author lirui
 * @since 2020-06-13
 */
@RestController
@RequestMapping("user")
public class UserController extends YaoBaseController<UserParam, UserEntity, UserDTO> {


    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private ResourceService resourceService;

    @Override
    public YaoBaseService<UserParam, UserEntity> getService() {
        return userService;
    }

    @Override
    public UserDTO getDTO() {
        return new UserDTO();
    }

    @Override
    public UserEntity getEntity() {
        return new UserEntity();
    }

    /**
     * 获取用户信息包括权限和表单
     *
     * @param token
     * @return
     */
    @RequestMapping("/userInfo")
    public ResponseBean userAuth(@RequestHeader("Authorization") String token) {
        String username = UserContentHolder.getContext().getUsername();

        //获取当前用户所拥有的角色
        List<String> userRoles = roleService.getUserRoles(username);
        //获取当前用户的权限
        Set<String> userAuthorities = resourceService.getUserAuthorities(userRoles);

        //获取当前用户所拥有的菜单
        List<ResourceEntity> userMenus = resourceService.getUserMenus(userRoles);

        UserEntity userEntity = userService.findByUsername(username);
        Map<String, Object> userInfo = new HashMap<>(8);
        userInfo.put("allMenuUrls", resourceService.getAllMenuUrls(userRoles));
        userInfo.put("menus", userMenus);
        userInfo.put("authorities", userAuthorities);
        userInfo.put("roles", userRoles);
        userInfo.put("username", username);
        userInfo.put("nickname", userEntity.getNickname());
        ResponseBean listResponseBean = ResponseBean.builder().content(userInfo).build();
        return listResponseBean;
    }

    /**
     * 所有角色
     *
     * @return
     */
    @RequestMapping("/roles/{userName}")
    public ResponseBean listAllRoles(@PathVariable("userName") String userName) {
        Map<String, Object> map = new HashMap<>(16);

        List<KeyValue> roleAll = roleService.findAll().stream().map(roleEntity -> {
            KeyValue keyValue = new KeyValue();
            keyValue.setId(roleEntity.getRoleCode());
            keyValue.setText(roleEntity.getRoleName());
            return keyValue;
        }).collect(Collectors.toList());
        map.put("roles", roleAll);
        map.put("hasRoles", roleService.getUserRoles(userName));
        return successBean(map);
    }

    /**
     * 给用户分配角色
     *
     * @param userDTO
     * @return
     */
    @RequestMapping("/userRole")
    public ResponseBean userRole(@RequestBody UserDTO userDTO) {
        String username = userDTO.getUsername();
        userService.userRole(username, userDTO.getRoleCodes());
        return successBean();
    }

    /**
     * 管理员重置密码
     *
     * @param id
     * @return
     */
    @GetMapping("/resetPwd/{id}")
    public ResponseBean resetPwd(@PathVariable("id") Long id) {
        userService.resetPwd(id);
        return successBean();
    }

    /**
     * 修改密码
     *
     * @param userDTO
     * @return
     */
    @RequestMapping("/updatePwd")
    public ResponseBean updatePwd(@RequestBody UserDTO userDTO) throws Exception {
        String userName = UserContentHolder.getContext().getUsername();

        userService.updatePwd(userName, userDTO.getNewPassword(), userDTO.getOldPassword());
        return successBean();
    }

    /**
     * 更改用户状态
     *
     * @param userName
     * @param status
     * @return
     */
    @RequestMapping("/operaUser/{userName}/{status}")
    public ResponseBean cancel(@PathVariable("userName") String userName, @PathVariable("status") Integer status) {
        String username = UserContentHolder.getContext().getUsername();
        userService.updateUserStatus(userName, status, username);
        return successBean();
    }

    /**
     * 查询用户下拉框
     *
     * @return
     */
    @GetMapping("/getUserList")
    public ResponseBean getUserList() {
        List<KeyValue> list = userService.getUserList();
        return ResponseBean.builder().content(list).build();
    }
}
